package com.power.home.common.apkUpdate;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.power.home.App;
public class AppUtils {
    /**
     * 获取应用程序名称
     */

    public static String getAppName(){
        try{
            PackageManager packageManager = App.getApplication().getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(App.getApplication().getPackageName(),0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return App.getApplication().getResources().getString(labelRes);
        }catch (Exception e){

        }
        return null;
    }
    /**
     * [获取应用程序版本名称信息]

     * @return 当前应用的版本名称
     */
    public static synchronized String getVersionName() {
        Context context = App.getApplication();
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * [获取应用程序版本名称信息]

     * @return 当前应用的版本名称
     */
    public static  int getVersionCode() {
        Context context = App.getApplication();
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * [获取应用程序版本名称信息]

     * @return 当前应用的版本名称
     */
    public static  String getPackageName() {
        Context context = App.getApplication();
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.packageName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取图标 bitmap

     */
    public static  Bitmap getBitmap() {
        Context context = App.getApplication();
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo = null;
        try {
            packageManager = context.getApplicationContext()
                    .getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(
                    context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        Drawable d = packageManager.getApplicationIcon(applicationInfo); //xxx根据自己的情况获取drawable
        BitmapDrawable bd = (BitmapDrawable) d;
        Bitmap bm = bd.getBitmap();
        return bm;
    }

}
